package com.hht.webpackagekit.inner;

import android.content.Context;
import android.util.Log;

import com.hht.webpackagekit.core.Constants;
import com.hht.webpackagekit.core.Downloader;
import com.hht.webpackagekit.core.PackageInfo;
import com.hht.webpackagekit.core.util.FileUtils;
import com.hht.webpackagekit.core.util.Logger;
import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadSampleListener;
import com.liulishuo.filedownloader.FileDownloader;
import com.liulishuo.filedownloader.model.FileDownloadStatus;

import java.io.File;
import java.util.function.LongUnaryOperator;

/**
 * 下载器实现类
 */
public class DownloaderImpl implements Downloader {
    private final Context context;
    private String downloadUrl;

    public DownloaderImpl(Context context) {
        this.context = context;
    }

    @Override
    public void download(PackageInfo packageInfo, final DownloadCallback callback) {
        downloadUrl = Constants.BASE_URL + packageInfo.getDownloadUrl();
        Logger.d("DownloaderImpl 开始下载 downloadUrl = " + downloadUrl + " packageInfo = " + packageInfo);

        String downloadPath = FileUtils.getPackageDownloadName(context, packageInfo.getPackageId(), packageInfo.getVersion());
        Logger.d("DownloaderImpl downloadPath = " + downloadPath);

        BaseDownloadTask downloadTask = FileDownloader.getImpl()
            .create(downloadUrl)
            .setTag(packageInfo.getPackageId())
            .setPath(downloadPath)//download.zip
            .setListener(new FileDownloadSampleListener() {
                @Override
                protected void completed(BaseDownloadTask task) {
                    super.completed(task);
                    Logger.d("DownloaderImpl 下载成功, packageId = " + task.getTag());
                    
                    // 下载离线包成功后，开始下载index.html
                    downloadIndexHtml(packageInfo, new DownloadCallback() {
                        @Override
                        public void onSuccess(String packageId, String indexHtmlPath) {
                            Logger.d("DownloaderImpl 下载 index.html 成功");
                            if (callback != null) {
                                callback.onSuccess((String) task.getTag(), indexHtmlPath);
                            }
                        }

                        @Override
                        public void onFailure(String packageId) {
                            Logger.e("DownloaderImpl 下载 index.html 失败");
                            if (callback != null) {
                                callback.onFailure((String) task.getTag());
                            }
                        }
                    });
                }

                @Override
                protected void error(BaseDownloadTask task, Throwable e) {
                    super.error(task, e);
                    Logger.e("DownloaderImpl  packageResource download error: " + e.getMessage());
                    if (callback != null) {
                        callback.onFailure((String) task.getTag());
                    }
                }
            });
        downloadTask.start();
    }

    private void downloadIndexHtml(PackageInfo packageInfo, final DownloadCallback callback) {
        String indexUrl = Constants.BASE_URL;
        String workPath = FileUtils.getPackageWorkName(context, packageInfo.getPackageId(), packageInfo.getVersion());
        String indexHtmlPath = workPath + File.separator + "index.html";

        BaseDownloadTask indexTask = FileDownloader.getImpl()
            .create(indexUrl)
            .setTag(packageInfo.getPackageId())
            .setPath(indexHtmlPath)
            .setListener(new FileDownloadSampleListener() {
                @Override
                protected void completed(BaseDownloadTask task) {
                    super.completed(task);
                    Logger.d("index.html下载成功");
                    if (callback != null && task.getStatus() == FileDownloadStatus.completed) {
                        callback.onSuccess((String) task.getTag(), indexHtmlPath);
                    } else if (callback != null) {
                        callback.onFailure((String) task.getTag());
                    }
                }

                @Override
                protected void error(BaseDownloadTask task, Throwable e) {
                    super.error(task, e);
                    Logger.e("index.html download error: " + e.getMessage());
                    if (callback != null) {
                        callback.onFailure((String) task.getTag());
                    }
                }
            });
        indexTask.start();
    }
}
